//
//  YWInspectionMissionsViewController.m
//  maintain
//
//  Created by Kyo on 16/9/22.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWInspectionMissionsViewController.h"
#import "YWMacro.h"
#import "UIColor+CustomColors.h"
#import "DLTabedSlideView.h"
#import "DLFixedTabbarView.h"
#import "Masonry.h"

#import "FTPopOverMenu.h"

#import "YWUnReadCount.h"
#import "YWEwinMenu.h"
#import "YWEwinManager.h"
#import "YWQrcodeManager.h"
#import "YWInspectionMissionManager.h"

//#import "YWMoreInspectionRecordTableViewController.h"
#import "YWAssignInspectionMissionTableViewController.h"
#import "YWMatchMissionsViewController.h"
#import "YWCreateTempMissionTableViewController.h"
#import "YWUnfinishInspectionTableViewController.h"
#import "YWFinishInspectionTableViewController.h"
#import "YWManageInspecLineTableViewController.h"
#import "YWNoQrcodeSelectLocationTableViewController.h"

@interface YWInspectionMissionsViewController ()<DLTabedSlideViewDelegate>
@property (nonatomic, weak) DLTabedSlideView* tabedSlideView;
@property (nonatomic, strong) NSMutableArray *menuNameArray;
@property (nonatomic, strong) NSMutableArray *menuIconArray;
@property (nonatomic, strong) UIButton *scanButton;
@property (nonatomic, strong) UIButton *noScanButton;
@end

@implementation YWInspectionMissionsViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = NSLocalizedString(@"inspection", nil);
    self.view.backgroundColor = [UIColor customBackgroudColor];
    if (self.menuNameArray.count) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(clickRightBarButtonEvent:event:)];
    }
    
    [self autoLayoutView];
}

- (NSMutableArray *)menuNameArray{
    if (!_menuNameArray) {
        NSArray *menu = [[YWEwinManager shareManager] ewinMenuWithMenuEnName:ewinMenuEnName_inspection];
        _menuNameArray = [NSMutableArray array];
        _menuIconArray = [NSMutableArray array];
        [menu enumerateObjectsUsingBlock:^(YWEwinMenu*  _Nonnull menu, NSUInteger idx, BOOL * _Nonnull stop) {
            NSString *name = [[YWEwinManager shareManager] menuNameWithPersimissionMenuName:menu.menuName];
            if ([name isEqualToString:NSLocalizedString(@"assignInspectionMission", nil)] || [name isEqualToString:NSLocalizedString(@"manageInspectionLine", nil)]) {
                [_menuNameArray addObject:name];
                [_menuIconArray addObject:[NSString stringWithFormat:@"%@",menu.menuIcon]];
            }
        }];
    }
    return _menuNameArray;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    // 禁用 iOS7 返回手势
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // 开启
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    }
}
- (DLTabedSlideView*)tabedSlideView
{
    if (!_tabedSlideView) {
        DLTabedSlideView* tabedSlideView = [[DLTabedSlideView alloc] init];
        tabedSlideView.delegate = self;
        tabedSlideView.baseViewController = self;
        tabedSlideView.tabItemNormalColor = [UIColor blackColor];
        tabedSlideView.tabItemSelectedColor = [UIColor customRedColor];
        tabedSlideView.tabbarTrackColor = [UIColor customRedColor];
        tabedSlideView.backgroundColor = [UIColor whiteColor];
        YWUnReadCount *totalCount = [[YWEwinManager shareManager] totalCount];
        NSString *title = totalCount.inspectionTotal>0?[NSString stringWithFormat:@"%@(%ld)",NSLocalizedString(@"unFinished", nil),(long)totalCount.inspectionTotal]:NSLocalizedString(@"unFinished", nil);
        DLTabedbarItem* unfinishItem = [DLTabedbarItem itemWithTitle:title image:nil selectedImage:nil];
        DLTabedbarItem* finishItem = [DLTabedbarItem itemWithTitle:NSLocalizedString(@"isOver", nil) image:nil selectedImage:nil];
        tabedSlideView.tabbarItems = @[ unfinishItem, finishItem ];
        [tabedSlideView buildTabbar];
        tabedSlideView.selectedIndex = 0;
        [self.view addSubview:tabedSlideView];
        _tabedSlideView = tabedSlideView;
    }
    return _tabedSlideView;
}

- (void)autoLayoutView
{
    [self.tabedSlideView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.mas_equalTo(self.view.mas_top);//.offset(64);
        make.left.mas_equalTo(self.view.mas_left);
        make.height.mas_equalTo(self.view.mas_height);//.offset(-64);
        make.width.mas_equalTo(self.view.mas_width);
    }];
    [self.view addSubview:self.noScanButton];
    [self.view addSubview:self.scanButton];
}

- (NSInteger)numberOfTabsInDLTabedSlideView:(DLTabedSlideView*)sender
{
    return self.tabedSlideView.tabbarItems.count;
}

- (UIViewController*)DLTabedSlideView:(DLTabedSlideView*)sender controllerAt:(NSInteger)index
{
    switch (index) {
        case 0: {
            YWUnfinishInspectionTableViewController* unfinishVC = [[YWUnfinishInspectionTableViewController alloc] init];
            return unfinishVC;
        }
        case 1: {
            YWFinishInspectionTableViewController* finishVC = [[YWFinishInspectionTableViewController alloc] init];
            return finishVC;
        }
        default:
            return nil;
    }
}

- (UIButton *)scanButton{
    if (!_scanButton) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        //[scanButton setImage:[UIImage imageNamed:@"inspection_normal"] forState:UIControlStateNormal];
        //scanButton.layer.masksToBounds = YES;
        [button setTitle:NSLocalizedString(@"scanQrcode", nil) forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor customBlueColor];
        [button addTarget:self action:@selector(handleScanBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(SCREEN_WIDTH/4, SCREEN_HEIGHT-64-KbottomViewH, SCREEN_WIDTH/4*3, KbottomViewH);
        _scanButton = button;
    }
    return _scanButton;
}
- (UIButton *)noScanButton{
    if (!_noScanButton) {
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:NSLocalizedString(@"tempInspection", nil) forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor customYellowColor];
        [button addTarget:self action:@selector(handleNoScanBtnEvent:) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake(0, SCREEN_HEIGHT-64-KbottomViewH, SCREEN_WIDTH/4, KbottomViewH);
        _noScanButton = button;
    }
    return _noScanButton;
}

- (void)handleNoScanBtnEvent:(id)sender{
    YWNoQrcodeSelectLocationTableViewController *noQrcodeSlecetLocationVC = [[YWNoQrcodeSelectLocationTableViewController alloc] init];
    noQrcodeSlecetLocationVC.maintenanceType = YWMaintenanceTypePatrol;
    noQrcodeSlecetLocationVC.isDoTempMission = YES;
    [self.navigationController pushViewController:noQrcodeSlecetLocationVC animated:YES];
}

- (void)handleScanBtnEvent:(id)sender{
    [[YWQrcodeManager shareManager] clickScanButtonWithType:YWQrcodeScanTypeNomall ViewController:self completionBlock:^(NSString *qrcodeId, QRCodeReaderViewController *qrCodeReaderViewController) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [qrCodeReaderViewController dismissViewControllerAnimated:NO completion:^{
                [self qrcodeSuccessWithQrcodeID:qrcodeId];
            }];
        });
    }];
}
-(void)qrcodeSuccessWithQrcodeID:(NSString*)qrcodeId{
    //巡查任务
    NSArray *inspectionMissions = [[YWInspectionMissionManager shareManager] matchInspectionMissionsWithQrcodeID:qrcodeId orLocationId:0];
    if (inspectionMissions.count>0) {
        YWMatchMissionsViewController *matchMissionVC = [[YWMatchMissionsViewController alloc] init];
        matchMissionVC.matchMissions = inspectionMissions;
        matchMissionVC.qrCodeId = qrcodeId;
        [self.navigationController pushViewController:matchMissionVC animated:YES];
    }else{
        YWCreateTempMissionTableViewController *tempMissionVC = [[YWCreateTempMissionTableViewController alloc] init];
        tempMissionVC.qrcodeId = qrcodeId;
        tempMissionVC.maintenanceType = YWMaintenanceTypePatrol;
        tempMissionVC.isLocationInspection = NO;
        tempMissionVC.isEquipmentInspection = YES;
        [self.navigationController pushViewController:tempMissionVC animated:YES];
    }
}

- (void)clickRightBarButtonEvent:(UIBarButtonItem *)sender event:(UIEvent *)event{
    [FTPopOverMenu showFromEvent:event
                        withMenu:self.menuNameArray
                  imageNameArray:self.menuIconArray
                       doneBlock:^(NSInteger selectedIndex) {
                           NSString *menuName = self.menuNameArray[selectedIndex];
                           if ([menuName isEqualToString:NSLocalizedString(@"assignInspectionMission", nil)]) {
                               YWAssignInspectionMissionTableViewController *assignInspectVC = [[YWAssignInspectionMissionTableViewController alloc] init];
                               [self.navigationController pushViewController:assignInspectVC animated:YES];
                           }else if ([menuName isEqualToString:NSLocalizedString(@"manageInspectionLine", nil)]){
                               YWManageInspecLineTableViewController *manageInspectLineVC = [[YWManageInspecLineTableViewController alloc] init];
                               [self.navigationController pushViewController:manageInspectLineVC animated:YES];
                           }
                       } dismissBlock:nil];
}

@end
